package Interrupt;

import org.junit.Test;

public class ThreadInterrupt {
    @Test
    public void NewThreadInterrupted() {
        // 新线程实例
        Thread thread = new Thread(() -> {
        });

        System.out.println("中断之前,线程状态: " + thread.getState() + ",线程中断状态" + thread.isInterrupted());

        // 中断
        thread.interrupt();

        System.out.println("中断之后,线程状态: " + thread.getState() + ",线程中断状态" + thread.isInterrupted());
    }

    @Test
    public void TerminatedThreadInterrupted() throws InterruptedException {
        // 新线程实例
        Thread thread = new Thread(() -> {
        });

        thread.start();
        thread.join();

        System.out.println("中断之前,线程状态: " + thread.getState() + ",线程中断状态" + thread.isInterrupted());

        // 中断
        thread.interrupt();

        System.out.println("中断之后,线程状态: " + thread.getState() + ",线程中断状态" + thread.isInterrupted());
    }

    @Test
    public void RUNNABLEThreadInterrupted() throws InterruptedException {
        // 新线程实例
        Thread thread = new Thread(() -> {
            while (true) {
                // do nothing
            }
        });
        thread.start();

        System.out.println("中断之前,线程状态: " + thread.getState() + ",线程中断状态" + thread.isInterrupted());

        // 中断
        Thread.sleep(1000);//等到thread线程被中断之后
        thread.interrupt();

        System.out.println("中断之后,线程状态: " + thread.getState() + ",线程中断状态" + thread.isInterrupted());
    }

    @Test
    public void BLOCKEDThreadInterrupted() throws InterruptedException {
        Object lock = new Object();
        Runnable runnable = () -> {
            synchronized (lock) {
                while (true) {
                    // do nothing
                }
            }
        };

        // 线程1去获取并一直占有锁
        Thread thread1 = new Thread(runnable);
        thread1.start();

        // 线程2去获取锁,会阻塞
        Thread thread2 = new Thread(runnable);
        thread2.start();

        Thread.sleep(1000);//等到thread线程被中断之后

        System.out.println("中断之前,线程状态: " + thread2.getState() + ",线程中断状态" + thread2.isInterrupted());

        // 中断
        thread2.interrupt();

        System.out.println("中断之后,线程状态: " + thread2.getState() + ",线程中断状态" + thread2.isInterrupted());
    }

    @Test
    public void WAITINGThreadInterrupted() throws InterruptedException {
        Runnable runnable = () -> {
            synchronized (this) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    System.out.println("线程在WAITING状态,调用interrupt(),抛出异常");
                }
            }
        };

        // 让线程一直等待
        Thread thread = new Thread(runnable);
        thread.start();

        Thread.sleep(1000);//等到thread线程被中断之后

        System.out.println("中断之前,线程状态: " + thread.getState() + ",线程中断状态" + thread.isInterrupted());
        Thread.sleep(500);
        // 中断
        thread.interrupt();

        System.out.println("中断之后,线程状态: " + thread.getState() + ",线程中断状态" + thread.isInterrupted());
    }
}
